//
//  CSGXCommodityPickerBLL.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/20.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXCommodityPickerBLL.h"

#import "TCommodityInfoDAL.h"
#import "TInventoryDAL.h"

@implementation CSGXCommodityPickerBLL

+ (TInventoryModel *)loadInventoryModelWithCommCategory:(NSString *)category {
    TCommodityInfoModel *commModel = [TCommodityInfoDAL loadModelWithCategory:category];
    return [TInventoryDAL loadModelWithCommNo:commModel.cCoNo];
}

///修改库存数量
+ (NSString *)modifyCountWithCountStr:(NSString *)originCountStr index:(NSInteger)index count:(NSInteger)count {
    //
    NSString *countStr = @"";
    NSMutableArray *iCountArrM = [NSMutableArray arrayWithArray:[originCountStr componentsSeparatedByString:@","]];
    if (iCountArrM.count == 0) {
        return nil;
    }
    
    if (index != -1) {
        iCountArrM[index] = @(count).description;
    }
    for (int i = 0; i < iCountArrM.count; i++) {
        if (index == -1) {
            countStr = [countStr stringByAppendingFormat:@"%d,", 0];
        } else {
            countStr = [countStr stringByAppendingFormat:@"%@,", iCountArrM[i]];
        }
    }
    return countStr = [countStr subStringFrontOfLastIndex:1];
}


+ (TInventoryModel *)loadInventoryModelWithCommNo:(NSString *)commNo {
    return [TInventoryDAL loadModelWithCommNo:commNo];
}

///判断盘点状态
+ (InventoryCheckType)getInventoryTypeWithCountStr:(NSString *)countStr originCountStr:(NSString *)originCountStr commNo:(NSString *)commNo {
    InventoryCheckType type = 0;
    NSString *unitConversion = [TCommodityInfoDAL loadModelWithCommNo:commNo].cUnitConversion;
    NSArray *unitConversionArr = [unitConversion componentsSeparatedByString:@","];
    
    NSArray *countArr = [countStr componentsSeparatedByString:@","];
    NSArray *originCountArr = [originCountStr componentsSeparatedByString:@","];
    NSInteger count = 0;
    NSInteger originCount = 0;
    
    if (countArr.count == 1) {
        count = [countArr[0] integerValue] * [[unitConversionArr[0] substringFromIndex:1] integerValue];
        originCount = [originCountArr[0] integerValue] * [[unitConversionArr[0] substringFromIndex:1] integerValue];
    }else if (countArr.count == 2) {
        
        count = ([countArr[0] integerValue] * [[unitConversionArr[0] substringFromIndex:1] integerValue] +
                 [countArr[1] integerValue]) * [[unitConversionArr[1] substringFromIndex:1] integerValue];
        
        originCount = ([originCountArr[0] integerValue] * [[unitConversionArr[0] substringFromIndex:1] integerValue] +
                       [originCountArr[1] integerValue]) * [[unitConversionArr[1] substringFromIndex:1] integerValue];
    }else if (countArr.count == 2) {
        
        count = (([countArr[0] integerValue] * [[unitConversionArr[0] substringFromIndex:1] integerValue] +
                  [countArr[1] integerValue]) * [[unitConversionArr[1] substringFromIndex:1] integerValue] +
                 [countArr[2] integerValue]) * [[unitConversionArr[2] substringFromIndex:1] integerValue];
        
        originCount = (([originCountArr[0] integerValue] * [[unitConversionArr[0] substringFromIndex:1] integerValue] +
                        [originCountArr[1] integerValue]) * [[unitConversionArr[1] substringFromIndex:1] integerValue] +
                       [originCountArr[2] integerValue]) * [[unitConversionArr[2] substringFromIndex:1] integerValue];
    }
    
    if (count > originCount) {
        type = InventoryCheckTypeGain; //盘盈
    }else if (count < originCount) {
        type = InventoryCheckTypeLoss; //盘亏
    }else if (count == originCount) {
        type = InventoryCheckTypeLevel; //盘平
    }else {
        type = InventoryCheckTypeNone; //未盘点
    }
    return type;
}


+ (InventoryCheckType)getInventoryTypeWithCountStr:(NSString *)countStr commNo:(NSString *)commNo {
    
    TInventoryModel *originInventoryModel = [CSGXCommodityPickerBLL loadInventoryModelWithCommNo:commNo];
    //
    InventoryCheckType checkType = [self getInventoryTypeWithCountStr:countStr
                                                       originCountStr:originInventoryModel.iInventoryCount
                                                               commNo:originInventoryModel.iCoNo];
    return checkType;
}

///取得所有库存，分类是否盘点和未盘点，返回总和。
+ (NSArray<TInventoryModel *> *)getCheckTypeInventoryList:(NSArray<TInventoryModel *> *)inventoryCheckedList {
    NSMutableArray<TInventoryModel *> *mList = [NSMutableArray<TInventoryModel *> arrayWithArray:inventoryCheckedList];
    
    NSArray<TInventoryModel *> *originList = [TInventoryDAL loadListWithUserNo:[CSUserDefaultsTool getUserNo] isDiff:NO];
    if (inventoryCheckedList == nil || inventoryCheckedList.count == 0) {
        
        [mList addObjectsFromArray:originList];
    }else {
        NSMutableArray<TInventoryModel *> *mTemp = [NSMutableArray<TInventoryModel *> array];
        for (int i = 0; i < originList.count; i++) {
            BOOL isIn = NO;
            for (int j = 0; j < inventoryCheckedList.count; j++) {
                if ([originList[i].iCoNo isEqualToString:inventoryCheckedList[j].iCoNo]) { //库存的数据已经盘点过
                    isIn = YES;
                    break;
                }
            }
            if (!isIn) {
                [mTemp addObject:originList[i]];
            }
        }
        
        [mList addObjectsFromArray:mTemp];
    }
    
    return mList.copy;
}


+ (NSArray<TInventoryModel *> *)getCheckedYesInventoryList:(NSArray<TInventoryModel *> *)inventoryCheckedList {
    NSMutableArray<TInventoryModel *> *mList = [NSMutableArray<TInventoryModel *> array];
    for (int i = 0; i < inventoryCheckedList.count; i++) {
        if (inventoryCheckedList[i].iIsChecked != 0) {
            [mList addObject:inventoryCheckedList[i]];
        }
    }
    return mList.copy;
}
















#pragma mark - update
+ (void)updateInventoryWithList:(NSArray<TInventoryModel *> *)inventoryList {
    
    for (int i = 0; i < inventoryList.count; i++) {
        //判断盘点状态
        TInventoryModel *originInventoryModel = [CSGXCommodityPickerBLL loadInventoryModelWithCommNo:inventoryList[i].iCoNo];
        NSInteger isCheckN = [CSGXCommodityPickerBLL getInventoryTypeWithCountStr:inventoryList[i].iInventoryCount
                                                                   originCountStr:originInventoryModel.iInventoryCount
                                                                           commNo:originInventoryModel.iCoNo];
        inventoryList[i].iIsChecked = isCheckN;
    }
    
    
    [TInventoryDAL updateDbWithInventoryList:inventoryList];
}















@end
